#include "include/client.h"
// 解决byte命名冲突问题
#define byte win_byte_override
#include "include/mysql.h"
#undef byte

// 全局变量
extern MysqlUser* mysql;

// 构造函数
Client::Client()
    : clientId(0) {}

// 析构函数
Client::~Client() {}


// CRUD 函数的实现 (这里只是示例，需要根据实际需求实现)
bool Client::AddClient(int64_t clientId,string clientName, string clientPhone,string passWord){
    // SQL语句
    std::stringstream sqlStream;
    sqlStream << "INSERT INTO client (clientId, clientName,clientPhone,passWord) VALUES ("
        << to_string(clientId) << ",'" << clientName <<"','" << clientPhone << "','" << passWord << "')";
    std::string sqlStr = sqlStream.str();
    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}
bool Client::DeleteClient(int64_t clientId) {
    std::stringstream sqlStream;
    sqlStream << "delete from client where clientId = " << to_string(clientId);
    std::string sqlStr = sqlStream.str();
    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}
bool Client::ModifyClient(int64_t clientId, map<string, string> entry) {
    map<string, string>::iterator it = entry.begin();
    std::stringstream sqlStream;
    sqlStream << "update client set " << it->first << " = '" << it->second << "' where clientId = " << to_string(clientId);
    std::string sqlStr = sqlStream.str();
    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}
Client* Client::SearchClient(int64_t clientId) {
    std::stringstream sqlStream;
    sqlStream << "select * from client where clientId = " << to_string(clientId);
    std::string sqlStr = sqlStream.str();

    vector<vector<string>> result = mysql->select(sqlStr);

    this->setClientId(stoll(result[0][0]));
    this->setClientName(result[0][1]);
    this->setClientPhone(result[0][2]);

    return this;
}
int64_t Client::SearchClient(string clientName, string passWord) {
    std::stringstream sqlStream;
    sqlStream << "select clientId from client where clientName = '" << clientName <<"' and passWord = '" << passWord<<"'";
    std::string sqlStr = sqlStream.str();
    vector<vector<string>> result = mysql->select(sqlStr);
    if (result.size()==0) {
        return -1;
    }
    return stoll (result[0][0]);
}

bool Client::SearchClientExist(string clientName, string clientPhone) {
    std::stringstream sqlStream;
    sqlStream << "select clientId from client where clientName = '" << clientName <<"'";
    std::string sqlStr = sqlStream.str();
    vector<vector<string>> result_name = mysql->select(sqlStr);

    std::stringstream sqlStream2;
    sqlStream2 << "select clientId from client where clientPhone = '" << clientPhone << "'";
    std::string sqlStr2 = sqlStream2.str();
    vector<vector<string>> result_phone = mysql->select(sqlStr2);

    if (result_name.size() == 0 && result_phone.size() == 0) {
        return true;
    }
    return false;
}

vector<Client> Client::SearchAllClient() {
    vector<Client> clients;
    string sqlStr = "select * from client";
    vector<vector<string>> results = mysql->select(sqlStr);

    // 对结果进行装载
    vector<vector<string>>::iterator it = results.begin();
    for (it; it != results.end(); it++) {
        vector<string>::iterator it2 = (*it).begin();

        Client p;
        p.setClientId(stoll (it2[0]));
        p.setClientName((it2[1]));
        p.setClientPhone(it2[2]);
        p.setPassWord(it2[3]);

        clients.push_back(p);
    }
    return clients;
}


// 订票
bool Client::BookTripTicket(unsigned int tripId) {
    // 获取当前时间
    time_t nowUnixTime;
    time(&nowUnixTime);

    // SQL语句
    std::stringstream sqlStream;
    sqlStream << "insert into booking (clientId,tripId,bookTime) values ("
        << to_string(this->getClientId()) <<", "<< tripId <<", " << to_string(nowUnixTime) << ")";
    std::string sqlStr = sqlStream.str();

    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}

// 退票
bool Client::RefundTripTicket(unsigned int tripId){
    // 获取当前时间
    time_t nowUnixTime;
    time(&nowUnixTime);

    // SQL语句
    std::stringstream sqlStream;
    sqlStream << "update booking set refundTime = " << to_string(nowUnixTime) << " where tripId = " << tripId << " and clientId = "<< to_string(this->getClientId());
    std::string sqlStr = sqlStream.str();

    if (!mysql->TransactionExecuteQuery(sqlStr)) {
        return false;
    }
    return true;
}

// 查询所有预订单
vector<Book> Client::SearchAllBookMsg() {
    vector<Book> books;
    // SQL语句
    std::stringstream sqlStream;
    sqlStream << "select * from booking where clientId = "<< to_string(this->getClientId()) << " and refundTime IS NULL";
    std::string sqlStr = sqlStream.str();

    vector<vector<string>> results = mysql->select(sqlStr);
    // 对结果进行装载
    vector<vector<string>>::iterator it = results.begin();
    for (it; it != results.end(); it++) {
        vector<string>::iterator it2 = (*it).begin();

        Book p;
        p.setBookingId(stoi(it2[0]));
        p.setClientId(stoll(it2[1]));
        p.setTripId(stoi(it2[2]));
        p.setParseBookTime(ParseTimeStyle(stoll(it2[3])));

        books.push_back(p);
    }
    return books;
}

// 查询所有退订单
vector<Book> Client::SearchAllRefundMsg() {
    vector<Book> books;
    // SQL语句
    std::stringstream sqlStream;
    sqlStream << "select * from booking where clientId = " << to_string(this->getClientId()) << " and refundTime IS Not NULL";
    std::string sqlStr = sqlStream.str();

    vector<vector<string>> results = mysql->select(sqlStr);
    // 对结果进行装载
    vector<vector<string>>::iterator it = results.begin();
    for (it; it != results.end(); it++) {
        vector<string>::iterator it2 = (*it).begin();

        Book p;
        p.setBookingId(stoi(it2[0]));
        p.setClientId(stoll(it2[1]));
        p.setTripId(stoi(it2[2]));
        p.setParseBookTime(ParseTimeStyle(stoll(it2[3])));
        p.setParseRefundTime(ParseTimeStyle(stoll(it2[4])));

        books.push_back(p);
    }
    return books;
}

// 查询用户是否已经预订
bool Client::IsBookTripTicket(unsigned int tripId) {
    std::stringstream sqlStream;
    sqlStream << "select * from booking where clientId = " << to_string(this->getClientId()) << " and tripId = "<< tripId << " and refundTime IS NULL";
    std::string sqlStr = sqlStream.str();

    vector<vector<string>> results = mysql->select(sqlStr);
    if (results.size() == 0) {
        return true;
    }
    return false;
}



// 成员变量访问和修改函数的实现
int64_t Client::getClientId() const {
    return clientId;
}

void Client::setClientId(int64_t id) {
    clientId = id;
}

string Client::getClientName() const {
    return clientName;
}

void Client::setClientName(const string& name) {
    clientName = name;
}

string Client::getClientPhone() const {
    return clientPhone;
}

void Client::setClientPhone(const string& phone) {
    clientPhone = phone;
}

string Client::getPassWord() const {
    return passWord;
}
void Client::setPassWord(const string& password) {
    passWord = password;
}
